/*
 * 一个超简单的jQuery对话框插件
 * 作者: 顾志兵
 * 日期: 2013年10月22日
 * License : Apache V2
 *
 * 本插件原理非常简单，其主要目的是为自己的个人网站使用，同时也用于交流、学习。除此之外，并无特别之处。
 *
*/

;(function($){
  var _top_z_index = 99999;
  var _is_modaling = false;
  var _id_attr = "dialog_id";

  var defaults = {
    close_callback : $.noop,
    confirm_callback : $.noop,
    cancel_callback : $.noop,
    destroy_callback : $.noop,
    is_modal : false,
    dialog_id : "pig-default-dialog",
    destroy_on_close : false,
    modal_color : "rgba(255,255,255,.7)",
    position: "center" //允许的值center、left-bottom-corner、right-bottom-corner
  };

  var dialogs = [];
 
  //对话框对象实例
  //参数为绑定对话框的jquery对象
  var Dialog = function(jObj,options){
    this.settings = $.extend({},defaults,options || {});
    this.settings.dialog_id = wrap_dialog_id(this.settings.dialog_id);
    jObj.data(_id_attr,this.settings.dialog_id).wrap('<div id="'+ this.settings.dialog_id + '" class="pig-dialog"><div class="dialog-body"></div></div>');
    this.jdom = $("#" + this.settings.dialog_id);
    $(".dialog-body",this.jdom).append('<span class="icon-close">×</span>');
    this.jdom.appendTo("body");
    jObj.css({"display" : "block"})
    var _self = this;
    $(".icon-close",this.jdom).click(function(){
      _self.close();
    });

    dialogs[this.settings.dialog_id] = this;
    this.is_opened = false;
  };
  function wrap_dialog_id(origin_id){
    return "_pig_dialog_" + origin_id;
  }

  Dialog.prototype.open = function(){
    if(this.is_opened) return;
    var coordinate = get_coordinate(this);
    this.jdom.css(coordinate);
    this.jdom.css({"z-index" : _top_z_index,"display":"block"});
    if(this.settings.is_modal && !_is_modaling){
      var modal_div = $('<div class="pig-dialog-modal"></div>');
      var scroll_width = document.documentElement.scrollWidth || document.body.scrollWidth;
      var scroll_height = document.documentElement.scrollHeight || document.body.scrollHeight;
      modal_div.css({width : scroll_width,height : scroll_height});
      modal_div.appendTo('body');
      _is_modaling = true;
    }
    this.is_opened = true;
  };

  Dialog.prototype.close = function(){
    if(!this.is_opened) return;

    this.jdom.css("display","none");
    this.settings.close_callback.apply(this);
    if(this.settings.is_modal){
      $('.pig-dialog-modal').remove();
      _is_modaling = false;
    }
    this.is_opened = false;
    if(this.settings.destroy_on_close)
      this.destroy();
  };

  Dialog.prototype.destroy = function(){
    this.settings.destroy_callback.apply(this);
    this.jdom.remove();
    delete dialogs[this.settings.dialog_id];
  };

  //获取对话框ID，参数为绑定对话框的jquery对象
  var get_dialog_id = function(jObj){
    if(jObj.attr("dialogID")) return jObj.attr("dialogID");
    if(jObj.attr("id")) return jObj.attr("id");
    return defaults.default_dialog_id;
  };

  function get_browser_size(){
    return {
      width : document.documentElement.clientWidth || document.body.clientWidth,
      height : document.documentElement.clientHeight || document.body.clientHeight
    };
  }

  var get_coordinate = function(dialog){
    if(dialog.settings.coordinate) return dialog.settings.coordinate;

    dialog_width = dialog.jdom.outerWidth();
    dialog_height = dialog.jdom.outerHeight();
    var win_size = get_browser_size();
    var coordinate = {left:0,top:0};
    if(dialog.settings.position == "center"){
      coordinate.left = dialog_width >= win_size.width ? 0 : (win_size.width - dialog_width)/2;
      coordinate.top = dialog_height >= win_size.height ? 0 : (win_size.height - dialog_height)/2;
    }else if(dialog.settings.position == "left-bottom-corner"){
      coordinate.top = dialog_height >= win_size.height ? 0 : (win_size.height - dialog_height);
    }else if(dialog.settings.position == "right-bottom-corner"){
      coordinate.left = dialog_width >= win_size.width ? 0 : (win_size.width - dialog_width);
      coordinate.top = dialog_height >= win_size.height ? 0 : (win_size.height - dialog_height);
    }
    return coordinate;
  };

  $.fn.extend({
    PigDialog : function(options){
      var dialog_id = $(this).data(_id_attr);
      return dialogs[dialog_id] || new Dialog(this,options);
    }
  });

  $.extend({
    PigDialog : {
      "new" : function(options){
          if(!options) return false;
          if(dialogs[options.dialog_id]) return dialogs[dialog_id];

          if(!('destroy_on_close' in options)) options.destroy_on_close = true;
          var content = options.html ? $("<div>"+options.html+"</div>") : $("<div></div>");
          content.appendTo("body");
          return new Dialog(content,options);
        },
      "get" : function(dialog_id){
        return dialogs[wrap_dialog_id(dialog_id)];
      }
    }
  });
})(jQuery);
